﻿using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditNode : FormPage
    {
        private Lc_NodeBLL lnb = new Lc_NodeBLL();
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();

        protected override void BindPage()
        {
            BindProcess();
            if (FORM_IFEDIT)
            {
                Lc_Node lnd = lnb.GetInfoById(FORM_KEY);
                txtName.Text = lnd.C_NODE_NAME;
                ddlState.SelectedValue = lnd.N_NODE_STATE.ToString();
                ddlType.SelectedValue = lnd.N_NODE_TYPE.ToString();
                ddlProcess.SelectedValue = lnd.N_PROCESS_ID.ToString();
                txtXZTime.Text = lnd.N_TRANSACTION_TIME.ToString();
            }
            lblCzRen.Text = lognUser.USER_NAME;
            lblCzTime.Text = DateTime.Now.ToString();
        }

        private void BindProcess()
        {
            FORM_DOIING_DATE.Text = "0";
            txtXZTime.Text = "0";
            DataTable table = lpb.GetAllInfo("", "");
            if (table != null && table.Rows.Count > 0)
            {
                ddlProcess.DataSource = table;
                ddlProcess.DataTextField = "C_PROCESS_NAME";
                ddlProcess.DataValueField = "N_PROCESS_ID";
                ddlProcess.DataBind();
                ddlProcess.Items.Insert(0, new ListItem("---请选择---", "-1"));
            }
            else
            {
                MessageBox.Show(Page, "请添加流程信息");
                //btnSave.Enabled = false;
                return;
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            #region 验证数据

            //节点名称
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show(this, "节点名不能为空");
                txtName.Focus();
                return;
            }
            //节点状态
            if (ddlState.SelectedValue.ToString().Equals("-1"))
            {
                MessageBox.Show(this, "请选择一个节点状态");
                ddlState.Focus();
                return;
            }
            //节点类型
            if (ddlType.SelectedValue.ToString().Equals("-1"))
            {
                MessageBox.Show(this, "请选择一个节点类型");
                ddlType.Focus();
                return;
            }
            //节点所属流程
            if (ddlProcess.SelectedValue.ToString().Equals("-1"))
            {
                MessageBox.Show(this, "请选择一个节点所属流程");
                ddlType.Focus();
                return;
            }

            #endregion 验证数据

            Lc_Node model;

            #region 初始化对象

            if (FORM_IFEDIT)
            {
                model = lnb.GetInfoById(FORM_KEY);
            }
            else
            {
                model = new Lc_Node();
            }

            #endregion 初始化对象

            #region 绑定数据

            model.C_NODE_NAME = txtName.Text;
            model.N_DOIING_TIME = Convert.ToDecimal(FORM_DOIING_DATE.Text);
            model.N_NODE_STATE = decimal.Parse(ddlState.SelectedValue.ToString());
            model.N_TRANSACTION_TIME = Convert.ToDecimal(txtXZTime.Text);
            model.N_NODE_TYPE = decimal.Parse(ddlType.SelectedValue.ToString());
            model.N_PROCESS_ID = decimal.Parse(ddlProcess.SelectedValue);
            model.N_NODE_OPERATOR_ID = CurrentUserID;
            model.N_NODE_OPERATION_TIME = DateTime.Now;

            #endregion 绑定数据

            if (FORM_IFEDIT == true)
            {
                /*
                DataTable dt = lnb.GetAllInfo("NodeName", txtName.Text);
                if (dt.Rows.Count > 0)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        if (dt.Rows[0]["N_NODE_ID"].ToString() != Request.QueryString["ID"].ToString())
                        {
                            MessageBox.Show(Page, "该节点已经存在，不能更改为该节点");
                            return;
                        }
                    }
                }*/

                if (lnb.UPDATE(model, lognUser.USER_NAME) > 0)
                {
                    MessageBox.Show(Page, "保存成功");
                    return;
                }
                else
                {
                    MessageBox.Show(Page, "保存失败");
                    return;
                }
            }
            else
            {
                DataTable dt = lnb.GetAllInfoSel(txtName.Text, ddlProcess.SelectedValue);
                if (dt != null && dt.Rows.Count > 0)
                {
                    MessageBox.Show(Page, "该节点已经存在！");
                    return;
                }

                if (lnb.INSERT(model, lognUser.USER_NAME) > 0)
                {
                    MessageBox.Show(Page, "新增成功");
                    //FORM_IFEDIT =true;
                    //FORM_KEY = model.N_NODE_ID.ToString();

                    //var Builder = new StringBuilder();
                    //Builder.AppendLine("   var option={");
                    //Builder.AppendLine("                Msg:\"【温馨提示】添加成功！\",");
                    //Builder.AppendLine("                YesCallBack:function(index){");
                    //Builder.AppendLine("                                FORM_UPDATE_TITLE(\"编辑节点\");");
                    //Builder.AppendLine("                                top.$(\"#layui-layer\" + index + \" .layui-layer-close\").click();");
                    //Builder.AppendLine("                }");
                    //Builder.AppendLine("   };");
                    //Builder.AppendLine("   top.MessageBox.Alert(option);");
                    //MessageBox.ResponseScript(this, Builder.ToString());
                    return;
                }
                else
                {
                    MessageBox.Show(Page, "保存失败");
                    return;
                }
            }
        }
    }
}